
import java.util.ArrayList;

import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.joints.JointDef;
import org.jbox2d.dynamics.joints.MouseJoint;
import org.jbox2d.dynamics.joints.MouseJointDef;

import pulpcore.Input;
import pulpcore.sprite.ImageSprite;
import pulpfizz.physics.BodyUtils;
import pulpfizz.physics.Material;
import pulpfizz.physics.NewWorld;
import pulpfizz.physics.PhysicsUtils;
import pulpfizz.pulp.CameraControlledScene;
import pulpfizz.pulp.body.BodySprite;
import pulpfizz.pulp.body.Java2DBodySprite;
import pulpfizz.pulp.body.PhysicsLayer;
import pulpfizz.pulp.ui.SmoothMouse;

public class GregSucks extends CameraControlledScene
{
	private SmoothMouse sm;
	private MouseJoint mj;
	private PhysicsLayer physics;

	@Override
	public void load()
	{
		super.load();

		add(new ImageSprite("background.png", 0, 0));

		camera.zoomTo(0.9f);
		camera.fastForward();

		physics = new PhysicsLayer(30, 0, 0, 0, 0);
		physics.fitToStage();
		add(physics);

		createWorld();
	}

	public void createWorld()
	{
		NewWorld world = physics.getWorld();

		Material m;
		Body b;

		m = Material.STONE();

		physics.add(new Java2DBodySprite(BodyUtils.createBoundingBox(
			world, 1), physics));

		for (int i = 0; i < 10; i++)
		{
			b = BodyUtils.createCircle(
				world, 3, 3, 0.5f);
			m.applyAll(b);
			physics.add(new Java2DBodySprite(b, physics));
		}

		m = Material.WOOD();
		ArrayList<Body> bodies = BodyUtils.createBridge(
			world, new Vec2(-3, 0), new Vec2(1, 0), .4f, .4f, 1f);
		for (Body body : bodies)
		{
			m.applyAll(body);
			physics.add(new Java2DBodySprite(body, physics, 2));
		}

		/*
		 * Mouse stuff.
		 */
		b = BodyUtils.createCircle(
			world, -6, 0, 0.5f);
		m.applyAll(b);

		b.setBullet(true);
		BodySprite bs = new Java2DBodySprite(b, physics);
		physics.add(bs);
		b.allowSleeping(false);
		sm = new SmoothMouse();
		add(sm);
		setCursor(Input.CURSOR_HAND);
		MouseJointDef md = new MouseJointDef();
		md.body1 = world.getGroundBody();
		md.body2 = b;
		md.maxForce = 250.0f * PhysicsUtils.getWeight(b);
		md.dampingRatio = 0.5f;
		md.frequencyHz = 4.5f;
		mj = (MouseJoint) world.createJoint(md);
		mj.m_localAnchor = new Vec2(0, 0);
		mj.setTarget(b.getPosition());
		//		mj.setTarget(new Vec2(-2,5));

		//		JointBreaker jb = new JointBreaker(mj, 1000);
		//		jb.setAngleLimits(0, Math.PI/10);
		//		physics.add(jb);

		//        physics.add(new Java2DBodySprite(PhysicsUtils.createCircle(world,2,2,1,m),physics));
		//        Body prevBody = null;
		//        
		//        
		//        b = PhysicsUtils.createRoundRectangle(world, 3, 4, 3, 2, .25f, m);
		//        b.setBullet(true);
		//        physics.add(new Java2DBodySprite(b,physics));
		//        
		//        b = PhysicsUtils.createRoundStick(world, new Vec2(3,3),new Vec2(5,8),.5f,m);
		//        b.setBullet(true);
		//        physics.add(new Java2DBodySprite(b,physics));

		/*
				for (int i = 0; i < 2; i++)
				{
		//			Body body = PhysicsUtils.createCircle(world, i * 1.3f, 2 * i, 3f, m);
		            Body body = PhysicsUtils.createHollowBox(world, i * 3f+5, 2f * i+3, 1f,2f,0.5f,false);
					BodySprite sprite = new BodySprite(body,physics);
					physics.add(sprite);
		            m.applyAll(body);
					if (prevBody != null)
						PhysicsUtils.distanceJoint(body, prevBody);
					prevBody = body;
				}
		*/
	}

	@Override
	public void update(int elapsedTime)
	{
		super.update(elapsedTime);
		/*
		 * Target the mouseJoint to the physics-coordinates of the current mouse location.
		 */
		if (physics != null)
		{
			double pX = physics.getPhysicsX(
				sm.x.get(), sm.y.get());
			double pY = physics.getPhysicsY(
				sm.x.get(), sm.y.get());
			if (pX > -physics.getPhysicsWidth() / 2
					&& pY > -physics.getPhysicsHeight() / 2)
			{

				if (mj != null)
				{
					mj.m_target.set(
						(float) pX, (float) pY);
				}
			}
		}
		//		System.out.println(mj.getReactionForce().length());
	}
}
